package com.station.Activitys.http;

import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.TelephonyManager;

import com.station.Activitys.utils.Constant;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 项目名称：基站 手机app
 * 类描述：联网相关的常量
 * 创建人：张飞祥
 * 创建时间：2018/04/18 下午2:17
 * 修改人：张飞祥
 * 修改时间：2018/04/18 下午2:17
 * 修改备注：
 */
public class HttpConstant {

    public static final String hTTP_URl = Constant.HTTP_URL;

    /**
     * user token
     **/
    @SuppressLint({"HardwareIds", "MissingPermission"})
    public static final String getUserToken(Context context) {
        TelephonyManager telephonyMgr = (TelephonyManager) context
                .getSystemService(context.TELEPHONY_SERVICE);
        return telephonyMgr.getDeviceId();
    }


    /**
     * user token  code
     **/
    public static final String getUserTokenCode(Context context) {
        String code = md5(getUserToken(context) + "Dline");
        return code;
    }


    /**
     * TimeStamp
     **/
    public static final String getTimeStamp() {
        return String.valueOf(System.currentTimeMillis());
    }


    /** MD5加密 **/
    public static String md5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5")
                    .digest(plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有md5这个算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code.toUpperCase();
    }
}
